﻿using PrintLabel.Model;
using PrintLabel.Tools;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 纺织贸易管理系统;

namespace PrintLabel.View
{
    public partial class Uncoiling : AntdUI.Window
    {
        private JuanHaoTable Roll=new JuanHaoTable();
        public Uncoiling(JuanHaoTable roll)
        {
            InitializeComponent();
            Roll=roll;
        }

        private void input1_Click(object sender, EventArgs e)
        {
            KeyboardHelper.ShowKeyboard("number", "输入");
        }

        private void txtDengji_Click(object sender, EventArgs e)
        {
            var fm = new SelectClassias();
            fm.ShowDialog();
            txtDengji.Text = fm.Grade;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty( txtDengji.Text))
            {
                ShowMessage.ShowErrorMessagebox(this, "请先选择等级");
                return;
            }
            if (string.IsNullOrEmpty(txtjuanhao.Text))
            {
                ShowMessage.ShowErrorMessagebox(this, "卷号不能为空");
                return;
            }
            Roll.biaoqianmishu-=txtLength.Text .ToDecimal();
            Roll.MiShu -= txtLength.Text.ToDecimal();
            try
            {
                using (var trans = dbContext.dbcontext.UseTran())
                {
                    var defects = dbContext.dbcontext.Queryable<ChiDianTable>().Where(x => x.JuanHao == Roll.JuanHao).ToList();
                    foreach (var item in defects)
                    {
                        if (item.WeiZhi.ToDecimal() > Roll.biaoqianmishu)
                        {
                            dbContext.dbcontext.Deleteable(item).ExecuteCommand();
                        }
                    }
                    dbContext.dbcontext.Updateable(Roll).ExecuteCommand();
                    var stock = RollHelper.QueryStockWithRoll(Roll);
                    stock.yijianjuanshu += 1;
                    dbContext.dbcontext.Updateable(stock).ExecuteCommand();
                    Roll.biaoqianmishu = txtLength.Text.ToDecimal();
                    Roll.MiShu = txtLength.Text.ToDecimal();
                    Roll.PiHao = txtjuanhao.Text.ToInt();
                    Roll.DengJI = txtDengji.Text;
                    Roll.JuanHao =RollHelper. CreateBarcode(Roll.GangHao,Roll.PiHao.ToString());
                    dbContext.dbcontext.Insertable(Roll).ExecuteCommand();
                    trans.CommitTran();
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                ShowMessage.ShowErrorMessagebox(this, "操作失败" + ex.Message);
                return;
            }
        }

        private void Uncoiling_Load(object sender, EventArgs e)
        {
            txtjuanhao.Text= RollHelper.CreatGangJuanhao(Roll).ToString();
        }
    }
}
